import QtQuick 1.1
import QtWebKit 1.0
import Qt 4.7
import "js/dbmanager.js" as DB
import "js/twitter-app.js" as APP
import "js/twitter-oauth.js" as TwitterOauth
import "../../../qml-ui/"
import "../../../qml-ui/js/app.js" as LIBAPP


Item {

    id: main
    property string title : ""
    property string socialMedia : "twitter"
    property bool dropDatabases: true  //if enabled, it will drob databases first
    property bool debug : false
    property string snapUrl: "http://twitter.com";
    property string textLoggedIn : "Congratulations, you are logged in";
    property string textLoggedOut : "You was loggged out from application";

    Component.onCompleted: {
        if(dropDatabases) DB.dropToken();
    }


    TwitterAuthentification{
        id: window

        onStateChanged: {
            if(state==DB.STATES.AUTHDONE){
                logoutButton.visible = true;
            }
            if(state==DB.STATES.LOGIN){
                logoutButton.visible = false;
            }
        }
    }

    states: [
        State {
            name: DB.STATES.LOGIN
            PropertyChanges { target: logoutButton; visible: false; }
        },
        State {
            name: DB.STATES.AUTHDONE
            PropertyChanges { target: webViewId; visible: true; }
            PropertyChanges { target: bg; visible: true;  }
            PropertyChanges { target: shareColumn; visible: true;  }
        }
    ]

    WebView {
        id: webViewId
        visible: false
        url: snapUrl
        anchors.fill: parent
    }

    Background{
        id: bg
    }


    Column{
        id: shareColumn
        width: parent.width - 64 * APP.scale
        spacing: 16 * APP.scale
        anchors.centerIn: parent
        visible: false

        LibraryImage{
            id: libImage
            image: "icon/twitter.jpg";
            visible: false
        }

        Image {
            id: image1

            source: libImage.source
            anchors.horizontalCenter: parent.horizontalCenter
        }

        Text {
            font.pixelSize: 26 * APP.scale;
            width: parent.width
            wrapMode: Text.WordWrap;
            color: "#ffffff"
            text: "Share on " + socialMedia
            horizontalAlignment: Text.AlignHCenter
        }

        Text {
            text:  title;
            font.pixelSize: 22 * APP.scale;
            width: parent.width
            wrapMode: Text.WordWrap;
            color: "#3B5998"
            horizontalAlignment: Text.AlignHCenter
        }

        FocusScope {
            id: focusScope
            width: parent.width
            height: 181 * APP.scale
            z: 10

            BorderImage {
                id: inactive
                source: APP.getImage("messageBox_bg.png");
                width: parent.width; height: parent.height
                border { left: 16 * APP.scale; top: 16 * APP.scale; right: 16 * APP.scale; bottom: 16 * APP.scale }
            }

            Text {
                id: typeSomething
                width: parent.width - 16 * APP.scale
                height: parent.height - 16 * APP.scale
                anchors.centerIn: parent;
                text: "Write your message"
                color: "gray"
                font.pixelSize: 22 * APP.scale
            }

            MouseArea {
                anchors.fill: parent
                onClicked: { focusScope.focus = true; reviewTextEdit.openSoftwareInputPanel(); }
            }

            TextEdit{
                id: reviewTextEdit
                property int maximumLength: 200
                property string previousText: text
                width: parent.width - 16 * APP.scale
                height: parent.height - 16 * APP.scale
                anchors.centerIn: parent;
                font.pixelSize: 22 * APP.scale;
                color: "#fff";
                wrapMode: TextEdit.WordWrap
                cursorPosition: text.length
                onTextChanged: {
                    if (text.length > maximumLength) {
                        var cursor = cursorPosition;
                        text = previousText;
                        if (cursor > text.length) {
                            cursorPosition = text.length;
                        } else {
                            cursorPosition = cursor-1;
                        }
                    }
                    previousText = text
                }
            }


            Rectangle {
                id: buttons

                anchors {
                    top: reviewTextEdit.bottom
                    topMargin: APP.margin
                    //                    horizontalCenter: parent.horizontalCenter
                }


                Button {
                    id: logoutButton
                    buttonImage: APP.getProjectImage("buttons/button_middleBlack_btn.png");
                    buttonImagePressed: APP.getProjectImage("buttons/button_middleBlack_pressed_btn.png");
                    buttonText: "Logout"
                    buttonTextColor: "#FFFFFF"
                    width: APP.buttonWidth * APP.scale
                    height: APP.buttonHeight * APP.scale
                    anchors{
                        left: parent.left
                        leftMargin: APP.halfMargin
                    }


                    onClicked: {
                        window.state=DB.STATES.LOGIN;
                        logoutButton.visible=false;
                        DB.deleteToken();
                        showNotification(textLoggedOut);

                    }
                }


                Button {
                    id: sendMessage
                    buttonImage: APP.getProjectImage("buttons/button_middleBlack_btn.png");
                    buttonImagePressed: APP.getProjectImage("buttons/button_middleBlack_pressed_btn.png");
                    buttonText: "Send message"
                    buttonTextColor: "#FFFFFF"
                    width: APP.buttonWidth * APP.scale
                    height: APP.buttonHeight * APP.scale
                    anchors{
                        left: logoutButton.right
                        leftMargin: APP.halfMargin
                    }

                    onClicked: {
                        if(reviewTextEdit.text!=""){
                            TwitterOauth.postToTwitter(reviewTextEdit.text);
                            reviewTextEdit.text = "";
                        }else{
                            var text = "!Fatal, text can't be empty";
                            console.log(text);
                            showNotification(text);
                        }
                    }
                }
            }

            states: State {
                name: "hasText"; when: reviewTextEdit.text != ''
                PropertyChanges { target: typeSomething; opacity: 0 }
            }

            transitions: [
                Transition {
                    from: ""; to: "hasText"
                    NumberAnimation { exclude: typeSomething; properties: "opacity" }
                },
                Transition {
                    from: "hasText"; to: ""
                    NumberAnimation { properties: "opacity" }
                }
            ]
        }
    }




    Button {
        id: quitButton
        buttonImage: APP.getProjectImage("buttons/button_middleBlack_btn.png");
        buttonImagePressed: APP.getProjectImage("buttons/button_middleBlack_pressed_btn.png");
        buttonText: "Quit"
        buttonTextColor: "#FFFFFF"
        width: APP.buttonWidth * APP.scale
        height: APP.buttonHeight * APP.scale

        anchors{
            bottom: parent.bottom
            bottomMargin: APP.margin
            right: parent.right;
            rightMargin: APP.halfMargin;
        }

        onClicked: {
            Qt.quit();
        }
    }


    Timer {
        id: timer
        interval: APP.timerInterval;
        repeat: true
        onTriggered: {
            notifications.visible = false;
        }
    }

    Notification {
        id: notifications
    }


    function showNotification(text) {
        notifications.text = text;
        notifications.show();
        notifications.visible = true;
        timer.start();
    }
}
